<template>
  <div>
    <div class="background-block">
      <div><h1 style="text-align: center">{{problem.title}}</h1></div>
    </div>
    <div class="background-block">
      <markdown-preview v-model="problem.content" style="width: 200px"/>
    </div>
    <el-button link>请作答</el-button>
    <hr/>
    <!-- 语言选择器 -->
    <div class="background-block">
      <select-lang v-model="commitCode.lang"/>
    </div>
    <!-- 代码 -->
    <CodemirrorEditor class="background-block" v-model="commitCode.code" :codeLang="commitCode.lang" :lang="'cpp'"
                      :style="{ height: '600px', fontSize: '18px' }"/>
    <div class="background-block">
      <el-button :loading="commitLoading" type="primary" style="font-weight: bold;" @click="commitCodeHandler">提交代码</el-button>
      <el-button @click="codeResultShow=!codeResultShow">提交结果</el-button>
    </div>
    <code-result v-model="codeResultShow" :code-result="commitCodeResult"/>
  </div>
</template>

<script>
import MarkdownPreview from '@/components/markdown-preview/markdown-preview'
import { ref } from 'vue'
import CodemirrorEditor from '@/components/codemirror-editor/CodemirrorEditor'
import SelectLang from '@/components/select-lang/select-lang'
import { problemById, problemRunUserCode } from '@/api/problem'
import CodeResult from '@/components/code-result/code-result'

export default {
  name: 'problem-detail',
  components: {
    CodeResult,
    SelectLang,
    CodemirrorEditor,
    MarkdownPreview
  },
  setup () {
    return {
      commitLoading: ref(false),
      codeResultShow: ref(false),
      problem: ref({
        content: '',
        code: '',
        title: '',
        labels: '',
        passNo: 0,
        id: 0,
        answerNo: 0
      }),
      commitCode: ref({
        problemId: 0,
        lang: 'java8',
        code: ''
      }),
      commitCodeResult: ref({})
    }
  },
  methods: {
    commitCodeHandler () {
      // todo fd
      // problemCommitCode()
      this.commitLoading = true
      console.log('代码', this.commitCode)
      problemRunUserCode(this.commitCode).then(value => {
        console.log('测试用例正确性验证', value)
        this.commitCodeResult = value.data
        this.codeResultShow = true
        this.commitLoading = false
      })
    }
  },
  mounted () {
    document.documentElement.scrollTop = 0
    const query = this.$router.currentRoute.value.query
    if (query) {
      const id = query.id
      this.commitCode.problemId = id
      if (id) {
        problemById(id).then(value => {
          console.log(value)
          this.problem = value.data
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
